package com.riguz.forks.framework.plugin.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.riguz.forks.framework.Framework;
import com.riguz.forks.framework.plugin.Plugin;
import com.riguz.forks.framework.plugin.PluginLoader;
import com.riguz.gags.base.Beans;
import com.riguz.gags.base.Classes;
import com.riguz.gags.io.Config;

public class DefaultPluginLoader extends PluginLoader{
    private static final Logger logger = LoggerFactory.getLogger(DefaultPluginLoader.class);
    
    @SuppressWarnings("unchecked")
    @Override
    public void registerPlugins() {
        
        List<String> pluginNames = (List<String>)Config.use(Framework.FRAMEWORK_SETTINGS).get("plugins");
        if(pluginNames == null){
            logger.warn("No plugin definations found.");
            return;
        }
        for(String pluginName:pluginNames){
            System.out.println(pluginName);
            Class<?> pluginClass = Classes.loadClass(pluginName);
            if(pluginClass == null)
                throw new IllegalArgumentException("Plugin class not found:" + pluginName);
            if(!Plugin.class.isAssignableFrom(pluginClass))
                throw new IllegalArgumentException("Plugin class is not a plugin:" + pluginName);
            Object instance = Beans.newInstance(pluginClass);
            this.registerPlugin((Plugin)instance);
        }
            
        
    }

}
